/* 
 * Copyright (c) 2010, Avdhesh yadav.
 * http://www.avdheshyadav.com
 * Contact: avdhesh.yadav@gmail.com
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

package com.avdy.p4j.sample.client.view;

import net.customware.gwt.presenter.client.EventBus;

import com.avdy.p4j.sample.client.SampleCSS;
import com.avdy.p4j.sample.client.presenter.MainPresenter;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class MainView extends Composite implements MainPresenter.Display
{
	private DockPanel dockPanel;
	private VerticalPanel north;
	private DecoratorPanel center;
	private Tree tree = new Tree();
	
	private DecoratorPanel west;
	private VerticalPanel treePanel = new VerticalPanel();
	private SimplePanel panel = new SimplePanel();
	private EventBus bus;
	
	@Inject
	public MainView(EventBus bus)
	{
		this.bus = bus;

		dockPanel = new DockPanel();
		dockPanel.setSpacing(3);
		dockPanel.setWidth("100%");

		createNorth();
		createWest();
		createCenter();
		
		dockPanel.add(north, DockPanel.NORTH);
        dockPanel.add(west, DockPanel.WEST);
        dockPanel.setCellWidth(west, "100px");
        dockPanel.add(center, DockPanel.CENTER);
        dockPanel.setCellHorizontalAlignment(north, DockPanel.ALIGN_RIGHT);
        dockPanel.setCellHorizontalAlignment(center, DockPanel.ALIGN_LEFT);

		initWidget(dockPanel);
	}

	
	/**
	 * 
	 */
	private void createNorth()
	{
		north = new VerticalPanel();
		north.setWidth("100%");
	}

	
	/**
	 * 
	 */
	private void createCenter()
	{
		center = new DecoratorPanel();
		center.setWidth("100%");
	}

	
	/**
	 * 
	 */
	private void createWest() 
	{
		west = new DecoratorPanel();
		west.add(tree);
		west.addStyleName(SampleCSS.C_tree_container);
		
		createMainMenu();
		
		tree.setAnimationEnabled(true);
		treePanel.setSpacing(5);

		treePanel.add(tree);
		panel.add(treePanel);
		west.add(panel);
	}
	
	/**
	 * 
	 */
	private void createMainMenu()
	{
	    tree.addItem(ALL_BOOKS);
	    tree.addItem(ISSUED_BOOKS);
	    tree.addItem(ISSUE_BOOK);
	    tree.addItem(RETURN_BOOK);
	}

	/**
	 * 
	 */
	public Widget asWidget()
	{
		return this;
	}

	/**
	 * 
	 */
	public HasSelectionHandlers<TreeItem> getTree()
	{
		return tree;
	}
	
	/**
	 * 
	 */
	public void addWidget(Widget widget)
	{
		center.clear();
		center.add(widget);
	}
	
	/**
	 * 
	 */
	public void removeWidget(Widget widget) 
	{
		center.remove(widget);
	}

	/**
	 * 
	 */
	public void showWidget(Widget widget)
	{
		center.clear();
		center.add(widget);
	}
}
